package com.jinlejia.feedback.idea.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jinlejia.feedback.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:下拉列表Adapter
 * @date :2019/6/26 15:36
 */
public class FBListMapDropDownAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Map<String, Object>> list;
    private int checkItemPosition = 0;
    private int tag;



    public void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataSetChanged();
    }

    public FBListMapDropDownAdapter(Context context, ArrayList<Map<String, Object>> list,int tag) {
        this.context = context;
        this.list = list;
        this.tag = tag;
    }

    public void setData( ArrayList<Map<String, Object>> list ){
        this.list = list;
        if (tag==1){
            Map<String,Object> map = new HashMap<>();
            map.put("valueName","处理状态");
            this.list.add(0,map);
            notifyDataSetChanged();
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("valueName","不限");
            this.list.add(0,map);
            notifyDataSetChanged();
        }


    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.fb_item_list_drop_down, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        fillValue(position, viewHolder);
        return convertView;
    }

    @SuppressLint("ResourceType")
    private void fillValue(int position, ViewHolder viewHolder) {
        viewHolder.tvName.setText(list.get(position).get("valueName").toString());
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.tvName.setTextColor(Color.parseColor("#ffffbb33"));
                viewHolder.ivFlag.setVisibility(View.VISIBLE);
            } else {
                viewHolder.tvName.setTextColor(Color.parseColor("#FF000000"));
                viewHolder.ivFlag.setVisibility(View.GONE);
            }
        }
    }


    class ViewHolder {
        TextView tvName;
        ImageView ivFlag;
        ViewHolder(View view) {
          tvName =  view.findViewById(R.id.tv_name);
          ivFlag = view.findViewById(R.id.iv_flag);
        }
    }
}
